using System;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using System.Data;
using IPAMS.Entities;

namespace IPAMS.Views
{
    /// <summary>
    /// This is an editor for the QuantityTransaction table. It enables loading from a file,
    ///saving to a file and more
    /// </summary>
    public partial class QuantityTransactionEditor : UserControl
    {
        public event EventHandler<TEventArgs<IAction>> TransactionAdded;

        public DSIPAMS.DTQuantityTransactionsDataTable transactions;
        string[] transactionsHeaders;

        /// <summary>
        /// Initializes a new instance of the <see cref="QuantityTransactionEditor"/> class.
        /// </summary>
        public QuantityTransactionEditor()
        {
            InitializeComponent();
            InitializeComponentExtended();
        }

        /// <summary>
        /// Initializes the component extended.
        /// </summary>
        private void InitializeComponentExtended()
        {
            transactions = new DSIPAMS.DTQuantityTransactionsDataTable();
        }

        /// <summary>
        /// Handles the Click event of the buttonAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            transactions.AddDTQuantityTransactionsRow(dateTimePicker.Value,radioButtonBuy.Checked ? "BUY" : "SELL",
                                                                               comboBoxStockSymbols.SelectedText, Convert.ToInt32(numericUpDownQuantity.Value),
                                                                                   comboBoxTransactionCurrency.Text);
            dgvQuantityTransaction.DataSource =  transactions;
            dgvQuantityTransaction.Refresh();

            if(TransactionAdded != null)
            {
               // IAction action;
                if(radioButtonBuy.Checked)
                {
                   // action = new BuyAction(dateTimePicker.Value,-1,new Stock());
                    
                }

            }
         
        }

        /// <summary>
        /// Handles the Click event of the buttonRemove control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonRemove_Click(object sender, EventArgs e)
        {
            foreach(DataGridViewRow selectedRow in dgvQuantityTransaction.SelectedRows)
            {
                dgvQuantityTransaction.Rows.Remove(selectedRow);
            }
        }


        /// <summary>
        /// Loads the data from a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        /// <returns>a DataTable with the file's data</returns>
        public DataTable LoadData(string dataFilePath)
        {
            if (File.Exists(dataFilePath))
            {
                StreamReader sr = new StreamReader(dataFilePath);
                transactionsHeaders = sr.ReadLine().Split(',');
                while (true)
                {
                    string transaction = sr.ReadLine();
                    if (transaction == string.Empty || transaction == null) break;
                    string[] transactionInfo = transaction.Split(',');
                    if (transactionInfo.Length != 5) continue;
                    CultureInfo ci = new CultureInfo("he-IL");
                   
                    transactions.AddDTQuantityTransactionsRow(DateTime.Parse(transactionInfo[0],ci.DateTimeFormat),
                                                              transactionInfo[1],
                                                              transactionInfo[2],
                                                              Convert.ToInt32(transactionInfo[3]), transactionInfo[4]);
                }
                sr.Close();

                transactions.AcceptChanges();
                dgvQuantityTransaction.DataSource = transactions;
                Text = dataFilePath;
                for (int index = 0; index < transactionsHeaders.Length; index++)
                {
                    dgvQuantityTransaction.Columns[index].HeaderText = transactionsHeaders[index];
                }
                foreach (DataGridViewColumn column in dgvQuantityTransaction.Columns)
                {
                    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                }
            }
            return transactions;
           
        }

        /// <summary>
        /// Saves the data to a file.
        /// </summary>
        /// <param name="dataFilePath">The data file path.</param>
        public void SaveData(string dataFilePath)
        {
            StreamWriter sw = new StreamWriter(dataFilePath);
            string fileContent = transactionsHeaders + Environment.NewLine;
            foreach (DSIPAMS.DTQuantityTransactionsRow transaction in transactions)
            {
                fileContent += string.Format("{0},{1},{2},{3}" + Environment.NewLine, transaction[0], transaction[1], transaction[2], transaction[3]);
            }
            sw.Write(fileContent);
            sw.Close();
            
        }

        /// <summary>
        /// Handles the Click event of the buttonApplyChanges control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonApplyChanges_Click(object sender, EventArgs e)
        {
            transactions.AcceptChanges();
        }

       
       
    }
}
